import express from "express";

const router = express.Router();
import { ContractModel } from "../../db/model.js"
import { cystoModel,sersModel,tongModel,monyModel} from "../../db/server/customer/my_customers.js"
router.get('/', (req, res) => {
    res.send('服务器主页面')
})

// 获取合同列表
router.get("/getContract", async (req, res) => {
    let data = await ContractModel.find()
    res.send({
        code: 200,
        data
    })
})
router.get("/getsers", async (req, res) => {
    let data = await sersModel.find()
    res.send({
        code: 200,
        data
    })
})
router.get("/gettong", async (req, res) => {
    let data = await tongModel.find()
    res.send({
        code: 200,
        data
    })
})
router.get("/getcystoModel", async (req, res) => {
    let data = await cystoModel.find()
    res.send({
        code: 200,
        data
    })
})
router.get("/getmony", async (req, res) => {
    let data = await monyModel.find()
    res.send({
        code: 200,
        data
    })
})
//添加模块
router.post("/addContract", async (req, res) => {
    await ContractModel.create(req.body)
    res.send("添加成功")
})
router.post("/addtong", async (req, res) => {
    console.log(req.body);
    await tongModel.create(req.body)
    res.send({
        code: 200,
        msg: "添加成功"
    })
})
router.post("/addcystoModel", async (req, res) => {
    console.log(req.body);
    await cystoModel.create(req.body)
    res.send({
        code: 200,
        msg: "添加成功"
    })
})
router.post("/addsersModel", async (req, res) => {
    await sersModel.create(req.body)
    res.send({
        code: 200,
        msg: "添加成功"
    })
})
router.post("/addmonyModel", async (req, res) => {
    console.log(req.body);
    await monyModel.create(req.body)
    res.send({
        code: 200,
        msg: "添加成功"
    })
})

//修改模块
router.put("/editcystoModel", async (req, res) => {
    console.log(req.body);
    let { _id,...data} = req.body
    await cystoModel.findByIdAndUpdate(_id, data)
    res.send({
        code: 200,
        msg: "修改成功"
    })
}
)
router.put("/edittong", async (req, res) => {
    console.log(req.body);
    let { _id,...data} = req.body
    await cystoModel.findByIdAndUpdate(_id, data)
    res.send({
        code: 200,
        msg: "修改成功"
    })
}
)
router.put("/editsersModel", async (req, res) => {
    console.log(req.body);
    let { _id,...data} = req.body
    await cystoModel.findByIdAndUpdate(_id, data)
    res.send({
        code: 200,
        msg: "修改成功"
    })
}
)
router.put("/editmonyModel", async (req, res) => {
    console.log(req.body);
    let { _id,...data} = req.body
    await monyModel.findByIdAndUpdate(_id, data)
    res.send({
        code: 200,
        msg: "修改成功"
    })
}
)

//删除模块
router.delete("/delcystoModel", async (req, res) => {
    let { _id } = req.body
    await cystoModel.deleteOne(_id)
    res.send({
        code: 200,
        msg: "删除成功"
    })
})
export default router;
